Inheritance
Reusing the properties of existing ones are called inheritance and derivation. C# does
not support the inheritance of multiple base classes into a single derived class.
Types of inheritance

Syntax:
class derived-class-name : base-class-name
{
// body of class
}

Note: Private members of the base class are not possible to inherit.

Program on Simple Inheritance
using System;
class abc
{
public int a;
public void get(int x)
{
a = x;
}
}
class xyz : abc
{
int b;
public void get1(int x)
{
b = x;
}
public void put()
{
Console.WriteLine("Sum of 2nos" + (a + b));
}
}
class sample
{
public static void Main(String[] args)
{
xyz x = new xyz();
x.get(3);
x.get1(2);
x.put();
}
}
Program to find the simple interest using Multileval Inheritance
using System;
class abc
{
public int p;
public void get(int x)
{
p = x;
}
}
class xyz : abc
{
public int t;
public void get1(int x)
{
t = x;
}

}
class pqr : xyz
{
int r;
public void get2(int x)
{
r = x;
}
public void put()
{
Console.WriteLine("Simple Interest" + (p * t * r) / 100);
}
}

 

class sample
{
public static void Main(String[] args)
{
pqr x = new pqr();
x.get(100);
x.get1(10);
x.get2(5);
x.put();
}
}
Program to find the net amount of 2 products using hierarchical inheritance
using System;
class abc
{
protected int ic,qty;
public void get(int x,int y)
{
ic = x;
qty = y;
}
}
class xyz : abc
{
public int rate;
public void get1(int x)
{
rate = x;
}
public void put()
{
Console.WriteLine("Item Code" + ic);
Console.WriteLine("Amount" + (qty * rate));
}
}
class pqr : abc
{
public int rate;
public void get1(int x)
{
rate = x;
}
public void put()
{
Console.WriteLine("Item Code" + ic);
Console.WriteLine("Amount" + (qty * rate));
}
}

class sample
{
public static void Main(String[] args)
{
xyz x = new xyz();
pqr p = new pqr();
x.get(100, 5);
x.get1(10);
x.put();
p.get(200, 10);
p.get1(100);
p.put();
}
}

Constructors in Inheritance
In a hierarchy, it is possible for both base classes and derived classes to have their own constructors. When only the derived class defines a constructor, the process is straightforward: simply construct the derived class object.
When both the base class and the derived class define constructors, the process is a bit more complicated because both the base class and derived class constructors must be executed. In this case you must use another of C#’s keywords:
base, which has two uses. The first use is to call a base class constructor. The second is to access a member of the base class that has been hidden by a member of a derived class.

A derived class can call a constructor defined in its base class by using an expanded form of the derived class’ constructor declaration and the base keyword. The general form of this expanded declaration is shown here:

derived-constructor(parameter-list) : base(arg-list)
{
// body of constructor
}

Program on Constructors in inheritance
using System;
class abc
{
public abc()
{
Console.WriteLine("This is base class constructor");
}

}
class xyz : abc
{
public xyz()
{
Console.WriteLine("this is derived class constructor");
}
}
class sample
{
public static void Main(String[] args)
{
xyz x = new xyz();
}
}

Program on parameterized constructors and inheritance
using System;
class abc
{
protected int a;
public abc(int x)
{
a = x;
}

}
class xyz : abc
{
int b;
public xyz(int x,int y):base(x)

    {
b = y;
}
public void put()
{
Console.WriteLine ("Sum"+(a+b));
}
}
class sample
{
public static void Main(String[] args)
{
xyz x = new xyz(10,20);
x.put();
}
}
Program on multilevel inheritance and constructors
using System;
class abc
{
protected int p;
public abc(int x)
{
p = x;
}

}
class xyz : abc
{
protected int t;
public xyz(int x,int y):base(x)

    {
t = y;
}

}
class pqr : xyz
{
int r;
public pqr(int x,int y,int z):base(x,y)
{
r=z;
}
public void put()
{
Console.WriteLine ("Simple interest"+(p*t*r)/100);
}
}
class sample
{
public static void Main(String[] args)
{
pqr x = new pqr(100,10,5);
x.put();
}
}